package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStoragePickingListActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStoragePickingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingListBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class PickingOutStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStoragePickingListBean.DataBean.PickingOutStorageListBean> mPickingOutStorageList;
    private LayoutInflater inflater;
    private OutStoragePickingListActivity.OutStoragePickingListener mOutStoragePickingListener;
    private String mPickingNumber, mCreator;

    public PickingOutStorageListAdapter(Context context, List<OutStoragePickingListBean.DataBean.PickingOutStorageListBean> pickingOutStorageList, String pickingNumber, String creator, OutStoragePickingListActivity.OutStoragePickingListener outStoragePickingListener) {
        mContext = context;
        mPickingOutStorageList = pickingOutStorageList;
        mOutStoragePickingListener = outStoragePickingListener;
        inflater = LayoutInflater.from(context);
        mPickingNumber = pickingNumber;
        mCreator = creator;
    }

    @Override
    public int getCount() {
        return mPickingOutStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mPickingOutStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_free_list_first_item, null);
            EditText pickingNumber = (EditText) convertView.findViewById(R.id.contractCode);
            EditText creator = (EditText) convertView.findViewById(R.id.purchase);
            Button search = (Button) convertView.findViewById(R.id.search);
            pickingNumber.setText(mPickingNumber);
            pickingNumber.setHint("请输入领料单/预留号/采购订单号");
            creator.setText(mCreator);
            creator.setHint("请输入创建人");
//            creator.setText("1");
            pickingNumber.addTextChangedListener(new QRCodeEditTextWatcher(pickingNumber, true) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, pickingNumber);
                    creator.requestFocus();
                }
            });
            creator.addTextChangedListener(new QRCodeEditTextWatcher(creator, true) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, creator);
                    creator.clearFocus();
                }
            });
            search.setOnClickListener(mOutStoragePickingListener.getNoDoubleClickListener(pickingNumber, creator));
        } else {
            OutStoragePickingListBean.DataBean.PickingOutStorageListBean pickingOutInstorageItem = mPickingOutStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_picking_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, pickingOutInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.pickingNumber = (TextView) view.findViewById(R.id.pickingNumber);
        viewHolder.createDate = (TextView) view.findViewById(R.id.createDate);
        viewHolder.creator = (TextView) view.findViewById(R.id.creator);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStoragePickingListBean.DataBean.PickingOutStorageListBean pickingOutStorageItem) {
        viewHolder.pickingNumber.setText(pickingOutStorageItem.getPickingNumber());
        viewHolder.createDate.setText(pickingOutStorageItem.getCreateDate());
        viewHolder.creator.setText(pickingOutStorageItem.getCreator());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStoragePickingOverviewActivity.class);
                intent.putExtra(OutStorageExtra.OUT_STORAGE_PICKING_NUMBER, viewHolder.pickingNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView pickingNumber, createDate, creator;
    }
}
